package com.chenchao.thread;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.*;

/**
 * @encoding : UTF-8
 * @Time : 2020/11/17 9:54
 * @Author : Chen Chao
 * @File : javaSE.com.chenchao.javathread.Test7.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 * 龟兔赛跑
 */
public class Test7  {
    public static int num=0;
    public static HashMap<Integer, String> integerStringHashMap = new HashMap<>();
    public static final int length = 1000;
    //赛道长度

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //创建服务
        ExecutorService executorService = Executors.newFixedThreadPool(4);
        //提交任务
        Future<Boolean> submit = executorService.submit(new Tortoise(2));
        Future<Boolean> submit1 = executorService.submit(new Tortoise(3));
        Future<Boolean> submit2 = executorService.submit(new Rabbit(15, 20));
        Future<Boolean> submit3= executorService.submit(new Rabbit(10, 10));
        //获取结果
        Boolean aBoolean = submit.get();
        Boolean aBoolean1 = submit1.get();
        Boolean aBoolean2 = submit2.get();
        Boolean aBoolean3 = submit3.get();
        //关闭服务
        executorService.shutdown();
        System.out.println(aBoolean);
        System.out.println(aBoolean1);
        System.out.println(aBoolean2);
        System.out.println(aBoolean3);

        System.out.println("比赛结束");
        Set<Map.Entry<Integer, String>> entries = integerStringHashMap.entrySet();
        for (Map.Entry<Integer, String> entry : entries) {
            System.out.println(entry.getValue() + "获得了第" + entry.getKey() + "名");
        }
    }
}

class Tortoise implements Callable<Boolean> {
    private int steps;
    private int runnedLength = 0;

    public Tortoise() {
    }

    public Tortoise(int steps) {
        this.steps = steps;
    }
    @Override
    public Boolean call() throws Exception {
        while (runnedLength < Test7.length) {
            System.out.println(Thread.currentThread().getName() + "跑了" + (runnedLength += steps));

        }
        synchronized (Test7.class) {
            String put = Test7.integerStringHashMap.put(++Test7.num, Thread.currentThread().getName());
            System.out.println(put+"=======================================================================================");
            System.out.println(Thread.currentThread().getName()+"到达了终点！++++++++++++++++++++++++++++++++++++++++++++++");
        }
        return Boolean.TRUE;
    }
}

class Rabbit implements Callable<Boolean> {
    private int runnedLength = 0;
    private int steps;
    private int sleepTime;


    public Rabbit() {
    }

    public Rabbit(int steps, int sleepTime) {
        this.steps = steps;
        this.sleepTime = sleepTime;
    }
    @Override
    public Boolean call() throws Exception {
        while (runnedLength < Test7.length) {
            System.out.println(Thread.currentThread().getName() + "跑了" + (runnedLength += steps));
            try {
                Thread.sleep(200);
                System.out.println(Thread.currentThread().getName() + "休息" + sleepTime + "ms");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        synchronized (Test7.class) {
            String put = Test7.integerStringHashMap.put(++Test7.num, Thread.currentThread().getName());
            System.out.println(put+"=======================================================================================");
            System.out.println(Thread.currentThread().getName()+"到达了终点！++++++++++++++++++++++++++++++++++++++++++++++");
        }
        return Boolean.TRUE;
    }
}
